package com.thirteen.app.controller;

import com.thirteen.app.dto.ExamRecordId;
import com.thirteen.app.dto.PaperRespose.AnswerDto;
import com.thirteen.app.dto.PaperRespose.PaperDto;
import com.thirteen.app.dto.PaperWithAnswerResponse.PaperWithAnswerDto;
import com.thirteen.app.dto.PaperWithScoreResponse.PaperWithScoreDto;
import com.thirteen.app.dto.PaperWithSolutionResponse.PaperWithSolutionDto;
import com.thirteen.app.service.ExamRecordService;
import com.thirteen.app.service.ExamService;
import com.thirteen.app.service.JudgeService;
import com.thirteen.app.service.PaperService;
import com.thirteen.app.utils.tools.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = {"试卷"})
@RestController
@RequestMapping("/api")
@CrossOrigin
public class PaperController {

    @Resource
    private PaperService paperService;

    @Resource
    private ExamRecordService examRecordService;

    @Resource
    private ExamService examService;

    @Resource
    private JudgeService judgeService;

    @ApiOperation("开始考试")
    @GetMapping("/student/paper/startExam")
    public CommonResult startExam(Integer examId, Integer userId) {

        Integer examRecordId = null;
        try {

            if (!examRecordService.canOpenNewExam(examId, userId)) {
                return CommonResult.failed("请完成当前进行的考试");
            }
            if (!examRecordService.isFirstAttend(examId, userId)) {
                if (!examRecordService.canRedoExam(examId, userId)) {
                    return CommonResult.failed("该考试的次数已用完");
                }
            }
            examRecordId = examService.startExam(examId, userId);
            if (examRecordId < 0) {
                return CommonResult.failed("非法操作");
            }
        } catch (Exception e) {
            return CommonResult.failed("非法操作");
        }
        PaperDto paperDto;
        try {
            paperDto = paperService.queryPaper(examRecordId);
        } catch (Exception e) {
            return CommonResult.failed("获取试卷失败");
        }
        return CommonResult.success(paperDto);
    }

    @ApiOperation("提交试题答案")
    @PostMapping("/student/paper/commitQu")
    public CommonResult submitQuestion(@RequestBody AnswerDto answerDto) {
        System.out.println("我被提交了啊！！！！！！！！！");
        paperService.submitAnswer(answerDto);
        return CommonResult.success("success");
    }

    @ApiOperation("提交试卷")
    @PostMapping("/student/paper/commitPaper")
    public CommonResult submitPaper(@RequestBody ExamRecordId eId) {
        Integer examRecordId = Integer.parseInt(eId.getExamRecordId());

        if (!paperService.submitPaper(examRecordId)) {
            return CommonResult.failed("提交试卷失败");
        }
        if (paperService.isMachineJudge(examRecordId) == true) {
            judgeService.judgeByMachine(examRecordId);
        }

        return CommonResult.success("提交试卷成功");
    }

    @ApiOperation("学生获取自己考过且被批阅了的试卷")
    @GetMapping("/student/examRecord/selectByExamRecordId")
    public CommonResult queryPaperWithScore(Integer examRecordId) {
        PaperWithScoreDto paperWithScoreDto;
        try {
            paperWithScoreDto = paperService.queryPaperWithScore(examRecordId);
        } catch (Exception e) {
            return CommonResult.failed("查看试卷失败");
        }
        return CommonResult.success(paperWithScoreDto);
    }

    @ApiOperation("查看学生考卷")
    @GetMapping("/teacher/exam/getExamPaper")
    public CommonResult queryPaperWithAnswer(Integer examRecordId) {
        PaperWithAnswerDto paperWithAnswerDto;
        try {
            paperWithAnswerDto = paperService.queryPaperWithAnswer(examRecordId);
        } catch (Exception e) {
            return CommonResult.failed("查看试卷失败");
        }
        return CommonResult.success(paperWithAnswerDto);
    }

    @ApiOperation("老师查看已经批阅过的试卷")
    @GetMapping("/teacher/examRecord/selectByExamRecordId")
    public CommonResult queryPaperAfterJudge(Integer examRecordId) {
        PaperWithScoreDto paperWithScoreDto;
        try {
            paperWithScoreDto = paperService.queryPaperWithScore(examRecordId);
        } catch (Exception e) {
            return CommonResult.failed("查看试卷失败");
        }
        return CommonResult.success(paperWithScoreDto);
    }

    @ApiOperation("管理员预览试卷")
    @GetMapping("/admin/exam/paper/selectQuListById")
    public CommonResult queryPaperPreview(Integer paperId) {
        PaperWithSolutionDto paperWithSolutionDto;
        try {
            paperWithSolutionDto = paperService.queryPaperWithSolution(paperId);
        } catch (Exception e) {
            return CommonResult.failed("查看试卷失败");
        }
        return CommonResult.success(paperWithSolutionDto);
    }

    @ApiOperation("老师获取自己创建的试卷列表")
    @GetMapping("/teacher/paper/paperList")
    public CommonResult queryAllPaper(Integer userId){
        return CommonResult.success(paperService.queryPaperListTeacher(userId));
    }

    @ApiOperation("管理员分页全查试卷")
    @GetMapping("/admin/exam/paper/pageList")
    public CommonResult queryAllPaperByLimit(String paperName,String courseName,Integer pageIndex, Integer pageSize) {
        return CommonResult.success(paperService.queryAllPaperByLimit(paperName,courseName,pageIndex, pageSize));
    }

    @ApiOperation("删除试卷")
    @GetMapping("/student/paper/delete")
    public CommonResult deletePaper(Integer paperId){
        return CommonResult.success(paperService.deletePaper(paperId));
    }

    @ApiOperation("管理员审核试卷")
    @PostMapping("/admin/exam/paper/changeAudit")
    public CommonResult judgePaperAuditAdmin(Integer paperId,Integer audit){
        return CommonResult.success(paperService.judgePaperAuditAdmin(paperId,audit));
    }

    @ApiOperation("老师提交审核试卷")
    @PostMapping("/teacher/paper/changeAudit")
    public CommonResult applyPaperAuditTeacher(Integer paperId){
        return CommonResult.success(paperService.applyPaperAuditTeacher(paperId));
    }

}
